package com.jiayang.business.equipmentinfopage;

import com.jiayang.base.equipment.StaEquipmentMapper;
import com.jiayang.base.equipment.domain.entity.StaEquipment;
import com.jiayang.base.sensor.StaSensorMapper;
import com.jiayang.base.sensor.domain.entity.StaSensor;
import com.jiayang.base.station.StaStationMapper;
import com.jiayang.base.station.domain.entity.StaStation;
import com.jiayang.business.equipmentinfopage.domain.dto.*;
import com.jiayang.common.domain.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：OuyangChenglong
 * @ Date       ：Created in 10:11 2021/9/8
 * @ Description：设备信息服务层
 * @ Modified By：
 * @Version: $
 */
@Service
public class EquipmentInfoService {

    @Autowired
    private StaEquipmentMapper staEquipmentMapper;

    @Autowired
    private StaSensorMapper sensorDao;

    @Autowired
    private EquipmentInfoDao equipmentInfoDao;

    @Autowired
    private StaStationMapper stationDao;

    public ResponseDTO getEquipmentImage(String id) {
        // 根据id获取设备图
        String imageUrl = staEquipmentMapper.getEquipmentImageById(id);
        // 根据设备id获取所有传感器坐标
        List<SensorLocationDTO> sensorLocationDTO = sensorDao.getXYById(id);
        EquipmentSensorDTO equipmentSensorDTO = new EquipmentSensorDTO(imageUrl, sensorLocationDTO);
        return ResponseDTO.succData(equipmentSensorDTO);
    }

    public ResponseDTO getSensorShortInfo(String id) {
        StaSensor StaSensor = sensorDao.selectById(id);
        String number = StaSensor.getNumber();
        String location = StaSensor.getLocation();
        String state = StaSensor.getState();
        SensorShortInfoDTO sensorShortInfoDTO = new SensorShortInfoDTO(number, location, state);
        return ResponseDTO.succData(sensorShortInfoDTO);
    }

    public ResponseDTO getShakeShortInfo(String id) {
        List<ShakeShortInfoDTO> list = sensorDao.getShakeShortInfo(id);
        return ResponseDTO.succData(list);
    }

    public ResponseDTO getInfraredShortInfo(String id) {
        InfraredShortInfoDTO infoDTO = sensorDao.getInfraredShortInfo(id);
        return ResponseDTO.succData(infoDTO);
    }


    public ResponseDTO getOilShortInfo(String id) {
        OilShortInfoDTO infoDTO = sensorDao.getOilShortInfo(id);
        return ResponseDTO.succData(infoDTO);
    }

    public ResponseDTO getSensorParameter(String id) {
        // 传感器参数来源未知待确认
        // TODO 传感器参数来源未知
        return null;
    }

    public ResponseDTO getElectricalParameter(String id) {
        List<ElectricParameterDTO> electricParameterDTO = equipmentInfoDao.getElectricalParameter(id);
        return ResponseDTO.succData(electricParameterDTO);
    }

    public ResponseDTO getEquipmentState(String id) {
        StaEquipment equipmentEntity = staEquipmentMapper.selectById(id);
        String state = equipmentEntity.getState();
        return ResponseDTO.succData(state);
    }

    public ResponseDTO getErrorInfo(String id) {
        List<EquipmentErrorInfoDTO> list = equipmentInfoDao.getErrorInfo(id);
        return ResponseDTO.succData(list);
    }

    public ResponseDTO getNamePlate(String id) {
        Map<String, String> map = new HashMap<>();
        StaEquipment equipmentEntity = staEquipmentMapper.selectById(id);
        String stationId = equipmentEntity.getStationId();
        StaStation stationEntity = stationDao.selectById(stationId);
        String name = stationEntity.getName();
        map.put("场站名",name);
        String unitName = equipmentEntity.getUnitName();
        map.put("机组名称",unitName);
        String unitNum = equipmentEntity.getUnitNum();
        map.put("机组编号",unitNum);
        LocalDateTime lastCheck = equipmentEntity.getLastCheck();
        String s = lastCheck.toString();
        map.put("上次检修时间",s);
        String runtime = equipmentEntity.getRuntime();
        map.put("当前开机时长",runtime);
        String wholeRuntime = equipmentEntity.getWholeRuntime();
        map.put("运行总时长",wholeRuntime);
        String mouthOnlineRate = equipmentEntity.getMouthOnlineRate();
        map.put("近一月运行率",mouthOnlineRate);
        return ResponseDTO.succData(map);
    }

    public ResponseDTO alarmHandling(AlarmHandlingFormDTO alarmHandlingFormDTO) {
        String reason = alarmHandlingFormDTO.getReason();
        String sensorTime = alarmHandlingFormDTO.getSensorTime();
        String time = alarmHandlingFormDTO.getTime();
        // TODO 一段时间内对此类型的问题不再报警
        return ResponseDTO.succMsg("处理成功");
    }


}
